package com.yq.demo.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yq.demo.domain.Demo;
import com.yq.demo.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Slf4j
@Controller
@RestController
public class DemoController {

    @Autowired
    private DemoService demoService;

    @GetMapping("/all")
    public List<Demo> getList(){
        PageHelper.startPage(1,10000);
        List<Demo> list = demoService.list();
        PageInfo<Demo> pageInfo = new PageInfo<>(list);
        return pageInfo.getList();
    }
    @GetMapping("/del/{id}")
    public void del(@PathVariable("id") Long id){
        log.info("删除成功");
        demoService.removeById(id);
    }
//    根据名字模糊查询
    @GetMapping("/getName/{name}")
    public List<Demo>  getName(@PathVariable("name") String name){
        log.info("查询信息成功");

        return demoService.findName(name);
    }
    @PostMapping("/update")
    public int update(
            @RequestBody
            Demo demo){
        log.info("更新成功");

        return   demoService.updateDemo(demo);
    }
    @PostMapping("/add")
    public void add(@RequestBody Demo demo){
        log.info("添加成功");
         demoService.addDemo(demo);
    }
    @GetMapping("/")
    public ModelAndView index(){
        return new ModelAndView("all");
    }
}
